package com.happyscan138.makefun;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioGroup;

import com.happyscan138.makefun.Urls.Urls;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by xiaok on 2017/1/20.
 */

public class setSpecial extends AppCompatActivity implements View.OnClickListener{
    private ImageButton back;
    private Button release;
    private String text;
    private RadioGroup radioGroup;
    private GifImageView GifA,GifB,GifC,GifD;
    private List<String> mdata;
    private String GifString;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_special);
        mdata=new ArrayList<String>();
        initView();
        getGifView();
    }
    public void initView(){
        back=(ImageButton)findViewById(R.id.setSpecial_Back);
        back.setOnClickListener(this);
        release=(Button)findViewById(R.id.setSpecial_release);
        release.setOnClickListener(this);
        GifA=(GifImageView)findViewById(R.id.special_answerA);
        GifB=(GifImageView)findViewById(R.id.special_answerB);
        GifC=(GifImageView)findViewById(R.id.special_answerC);
        GifD=(GifImageView)findViewById(R.id.special_answerD);
        radioGroup=(RadioGroup)findViewById(R.id.special_radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
             switch (checkedId){
                 case R.id.specialA:
                     text=mdata.get(0);
                     break;
                 case R.id.specialB:
                     text=mdata.get(1);
                     break;
                 case R.id.specialC:
                     text=mdata.get(2);
                     break;
                 case R.id.specialD:
                     text=mdata.get(3);
                     break;
             }
            }
        });

    }
    public void getGifView(){
        client.post(Urls.Special,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);

                String jsonStr=response.toString();
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject data=jsonArray.getJSONObject(i);
                        GifString=data.getString("sp_url");
                        mdata.add(GifString);


                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                File saveImgPath = getImageDir(setSpecial.this);
                File gifSavePath = new File(saveImgPath, "gif1");
                displayImage(base+mdata.get(0), gifSavePath, GifA);
                File saveImgPath2 = getImageDir(setSpecial.this);
                File gifSavePath2 = new File(saveImgPath2, "gif2");
                displayImage(base+mdata.get(1), gifSavePath2, GifB);
                File saveImgPath3 = getImageDir(setSpecial.this);
                File gifSavePath3 = new File(saveImgPath3, "gif3");
                displayImage(base+mdata.get(2), gifSavePath3, GifC);
                File saveImgPath4 = getImageDir(setSpecial.this);
                File gifSavePath4 = new File(saveImgPath4, "gif4");
                displayImage(base+mdata.get(3), gifSavePath4, GifD);
            }
        });


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.setSpecial_Back:
                finish();
                break;
            case R.id.setSpecial_release:
                Bundle bundle = new Bundle();
                bundle.putString("special",text);
                setResult(004,setSpecial.this.getIntent().putExtras(bundle));
                finish();
                log.e("special",text+"");
                break;
        }
    }
    public File getImageDir(Context context){
        File file = getFilesDir(context, "images");
        return file;
    }
    public File getFilesDir(Context context, String tag){
        if(isSdCardExist() == true){
            return context.getExternalFilesDir(tag);
        }
        else{
            return context.getFilesDir();
        }
    }
    public boolean isSdCardExist() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        }
        return false;
    }
    public void displayImage(String url, File saveFile, final GifImageView gifView){
        HttpUtils http = new HttpUtils();
        // 下载图片
        http.download(url, saveFile.getAbsolutePath(), new RequestCallBack<File>() {
            public void onSuccess(ResponseInfo<File> responseInfo) {
                try {
                    Log.e("tag", "onSuccess========" + responseInfo.result.getAbsolutePath());
                    GifDrawable gifFrom = new GifDrawable( responseInfo.result.getAbsolutePath() );
                    gifView.setImageDrawable(gifFrom);
                }
                catch(Exception e){
                    Log.e("tag", e.getMessage());
                }
            }

            @Override
            public void onFailure(com.lidroid.xutils.exception.HttpException e, String s) {
                Log.e("tag", "onFailure========" + s);
            }});
    }
}
